import React from 'react';
import { connect } from 'dva';
import { Link, routerRedux } from 'dva/router';
import { WingBlank, WhiteSpace, List, Flex, Toast, TextareaItem, Button, Checkbox, Icon } from 'antd-mobile';
import { createForm } from 'rc-form';
import MemberInfo from 'components/MemberInfo';
import { HypScaleReport, ScalePrograme } from 'components/scale';

const { Item, Item: { Brief } } = List;
const { CheckboxItem, AgreeItem } = Checkbox;

const NativeScaleHypReportPage = (props) => {
  const { dispatch, location, form, scalevalue, report, ...rest } = props;
  const { getFieldProps, getFieldError, validateFields } = form;
  const {Report1,patientvalue,} = location.state;
  const {ProgrameInt,ProgrameRec} = report;
  const ref = {Report1,patientvalue};
  const exitHandler = () => {
    // Toast.info('点击后退退出', 1);
    window.location = 'http://goBack';
  };

  const submitHandler = () => {
    validateFields((errors, value) => {
      if (errors) {
        const fieldNames = Object.keys(errors);
        if (fieldNames[0]) {
          return Toast.fail(getFieldError(fieldNames[0]));
        }
      }
      const {intids, recids} = props;
      if (recids.length > 1) { 
          return Toast.fail("养生方案只能选择一种");
      }else{
      const programeint = intids.join(';');
      const programerec = recids.join(';');

      const { type, doctorid, patientid, patientvalue } = location.state;

      const payload = {
        type,
        doctorid,
        patientid,
        patientvalue,
        Report1: report.Report1,
        programeint,
        programerec,
        // programe: Programe,
        ...value,
      };
      dispatch({
        type: 'scale/submitScalePrograme',
        payload,
      });
      Toast.success('保存绑定成功', 1);
      window.location = 'http://goBack';
    }
    }
    );

    
    
  };

  const paramReport = { Report1: Array.from(scalevalue[1]).join(';'), Report2: Array.from(scalevalue[0]).join(';'), Report3: Array.from(scalevalue[2]).join(';'), Report4: report.Report1 };
  
  return (
    <div>
      <MemberInfo {...location.state.member} />
      
      <WhiteSpace />
      <HypScaleReport {...ref} />
      <WhiteSpace />
      <ScalePrograme {...props} />
      
      <WingBlank size="md">
        <List>
          <Item extra={null}>
            <Flex justify="center">选择方案</Flex>
          </Item>
          {!ProgrameInt ? "" :
            ProgrameInt.map(item => (
            <AgreeItem
              key={item.Id}
              onChange={e =>
                dispatch({
                  type: 'scale/savePrograme',
                  payload: {
                    type: 'programeint',
                    value: item.Prog,
                  },
                })}
            >
              <Flex align="center" justify="between">
                {item.Prog}
                <Flex align="center">
                  <a href={item.Url} target="_blank">查看方案</a>
                  <Icon type="right" style={{ fill: '#ccc' }} />
                </Flex>
              </Flex>
            </AgreeItem>
          ))}
          {!ProgrameInt ? "" :
            ProgrameRec.map(item => (
            <AgreeItem
              onChange={e =>
                dispatch({
                  type: 'scale/savePrograme',
                  payload: {
                    type: 'programerec',
                    value: item.Prog,
                  },
                })}
            >
              <Flex align="center" justify="between">
                {item.Prog}
                <Flex align="center">
                  <a href={item.Url} target="_blank">查看方案</a>
                  <Icon type="right" style={{ fill: '#ccc' }} />
                </Flex>
              </Flex>
            </AgreeItem>
          ))}
          <Item extra={null}>
            备注：
            <WhiteSpace />
            <TextareaItem
              {...getFieldProps('comment', {
                initialValue: '',
              })}
              rows={5}
              style={{ border: '1px solid #ddd' }}
            />
          </Item>
          <WhiteSpace size="xl" />
          <WingBlank>
            <Flex>
              <Flex.Item>
                <Button onClick={exitHandler}>退出</Button>
              </Flex.Item>
              <Flex.Item>
                <Button type="primary" onClick={submitHandler}>保存</Button>
              </Flex.Item>
            </Flex>
          </WingBlank>
          <WhiteSpace size="xl" />
        </List>
      </WingBlank>
    </div>
  );
};

export default connect(({ scale }) => ({ ...scale }))(createForm()(NativeScaleHypReportPage));
